<?php
header('Content-Type: text/html; charset=utf-8');

//clan war clans = http://api.worldoftanks.eu/wot/globalwar/clans/?application_id=demo&map_id=1&page_no=1;
$requetes = array('cw' => array('api' => 'http://api.worldoftanks.eu/wot/globalwar/clans/?application_id=demo&map_id=1&page_no=%s',
                                'json' => '../json/clanwars_clan_%s.json'
                                ),
                  'clan' => array('api' => 'http://api.worldoftanks.eu/wot/clan/info/?application_id=demo&fields=victory_points_step_delta,members_count,name,color,abbreviation,emblems,motto,victory_points,description&clan_id=%s',
                                'json' => '../json/clan_data.json'
                                ),
                   'lang' => array('api' => 'http://api.loque.la/v1/?key=c8295c17da35d85227d68df60f31cfd0f1758ec6-4&q=%s',
                                   'json' => '../json/lang.json')
);

$index = 'api';
if($_SERVER['HTTP_HOST'] === 'localhost') {
    $index = 'json';
}

for ($i = 1; $i < 9999; $i++) {
    $json = file_get_contents(str_replace('%s', $i, $requetes['cw'][$index]));
    $clans = json_decode($json, true);
    if ($clans['status'] === 'ok' && $clans['count'] >0) {
        $activeClans = array();
        foreach ($clans['data'] as $clan) {
            $clandata[$clan['clan_id']]['provinces'] = $clan['provinces'];
            $activeClans[] = $clan['clan_id'];
        }
        $searchClans = implode(',', $activeClans);
        //detailsclans http://api.worldoftanks.eu/wot/clan/info/?application_id=demo&fields=victory_points_step_delta,members_count,name,color,abbreviation,emblems,motto,victory_points,description&clan_id=
        $lstData = json_decode(file_get_contents(str_replace('%s', $searchClans, $requetes['clan'][$index])), true);
        
        foreach ($lstData['data'] as $clanId => $dataClan) {
            $clandata[$clanId]['clanInfo'] = $dataClan;
            //$langs = json_decode(file_get_contents('http://api.loque.la/v1/?key=c8295c17da35d85227d68df60f31cfd0f1758ec6-4&q='.substr($dataClan['description'],100)),true);
            $clandata[$clanId]['clanInfo']['country'] = getClanCountry($dataClan['name'].' '.$dataClan['motto'].' '.$dataClan['description']);
        } 
        
        if ($clans['count'] < 100) {
            break;
        }
    }
    else {
        echo 'ko';
        break;
    }
}
echo "<table>";
echo "<tr><th>logo</th><th>Pays</th><th>Nom</th><th>membres<th>points</th></th><th>provinces</th>";
$a = $b = 0;
foreach ($clandata as $clanId => $data) {
    $b++;
    if ($data['clanInfo']['country'] !== 'eu') {
        $a++;
    }
    echo "<tr>";
        echo '<td><img src="'.$data['clanInfo']['emblems']['small'].'"</td>';
        echo '<td><img src="images/'.$data['clanInfo']['country'].'.png" /></td>';
        echo "<td>".$data['clanInfo']['abbreviation']."-".$data['clanInfo']['name']."</td>";
        echo "<td>".$data['clanInfo']['members_count']."</td>";
        echo "<td>".$data['clanInfo']['victory_points']."</td>";
        echo "<td>".count($data['provinces'])."</td>";
    echo "</tr>";
}
echo "</table>";
echo $a. ' / '.$b;

function getClanCountry($string) {
    global $requetes, $index;    
    $string = substr($string, 0, 200);
    $langs = json_decode(file_get_contents(str_replace('%s', $string, $requetes['lang'][$index])), true);
   
    $langs['data']['detections'][0][0]["language"];
    if (isset($langs['data']['detections'][0][0]["confidence"]) === true) {
        if ($langs['data']['detections'][0][0]["language"] !== 'en') {
            return $langs['data']['detections'][0][0]["language"];
        }
        return 'eu';
    }
    else {
        return 'eu';
    }
}	